﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Diagnostics;

namespace SlotRacer.Library.Controller
{
    public class Simulation : Base
    {
        const int MAJVER = 0;
        const int MINVER = 0;
        const int BUILDVER = 0;
        const int REVVER = 2;

        public Handle[] Handles {get; set; }

        System.Timers.Timer tickTimer = new System.Timers.Timer();

        public event EventHandler<HandleEventArgs> HandleChange;

        // Returns a unique GUID for this Slot-Car Controller system
        public override Guid Id
        {
            get { return new Guid("B5C0200A-CD35-4D6D-A78D-95851597B414"); }
        }

        // Returns common readable name for this Slot-Car Controller system
        public override string Name
        {
            get { return "SlotRacer Simulator"; }
        }

        // Returns version for this Slot-Car Controller system
        public override Version Version
        {
            get { return new Version(MAJVER, MINVER, BUILDVER, REVVER); }
        }

        // Starts processing input/output for this Slot-Car Controller system
        public override bool Connect()
        {
            Handles = new Handle[6];
            for (int i = 0; i < 6; i++)
                Handles[i] = new Handle();

            // Setup timer for simulating received / transmitted data loop 
            tickTimer.Interval = 10;
            tickTimer.Elapsed += tickTimer_Elapsed;
            tickTimer.Enabled = true;

            return true;
        }

        // Stops processing input/output for this Slot-Car Controller system
        public override bool Disconnect()
        {
            // Disable timer for simulating received / transmitted data loop 
            tickTimer.Enabled = false;

            return true;
        }

        void tickTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            SimulateInput();
        }

        void SimulateInput()
        {
            Random throttleRandom = new Random();

            // Simulate Throttle values
            foreach (Handle handle in Handles)
            {
                handle.Connected = true;
                handle.Throttle = throttleRandom.Next(0, 1000);
                handle.Break = throttleRandom.Next(0, 1000);
                handle.LaneChange = false;
                handle.LaneChangeLeft = false;
                handle.LaneChangeRight = false;

                // Raise change event
                EventHandler<HandleEventArgs> abc123 = HandleChange;
                if (abc123 != null)
                {
                    HandleEventArgs args = new HandleEventArgs();
                    args.Handle = handle;
                    abc123(this, args);
                }
            }

        }

        void SimulateOutput()
        {
        }
    }
}
